package com.itheima.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 王加鑫
 * @Date 2021/07/27 19:45
 * @Since JDK 1.8
 */
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {

        System.out.println("my exception is running ...."+ex);
        ModelAndView modelAndView = new ModelAndView();
        if(ex instanceof NullPointerException){
            modelAndView.addObject("msg","空指针异常");
        }else if(ex instanceof ArithmeticException){
            modelAndView.addObject("msg","算数运算异常");
        }else {
            modelAndView.addObject("msg","未知的异常");
        }
        modelAndView.setViewName("error.jsp");
        return modelAndView;
    }
}
